package com.cainaer.member.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cainaer.common.core.utils.StringUtils;
import com.cainaer.common.core.web.controller.BaseController;
import com.cainaer.common.core.web.domain.AjaxResult;
import com.cainaer.common.core.web.page.TableDataInfo;
import com.cainaer.member.domain.Member;
import com.cainaer.member.domain.vo.MemberVo;
import com.cainaer.member.domain.vo.Sign;
import com.cainaer.member.service.IMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 会员登录表 前端控制器
 * </p>
 *
 * @author Tina
 * @since 2020-08-22
 */
@RestController
@RequestMapping("/member/")
public class MemberController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(MemberController.class);
    @Autowired
    private IMemberService memberService;

    @PostMapping("/sign")
    public AjaxResult sign(@RequestBody Sign signService) throws IOException {
        logger.info("sign={}", signService);
        if (StringUtils.isEmpty(signService.getCode())) {
            return AjaxResult.error("CODE不能为空");
        }
        if (StringUtils.isNull(signService.getTelPhone())) {
            return AjaxResult.error("手机号码不能为空");
        }
        Sign sign = memberService.sign(signService);
        logger.info("标识sign={}", sign);
        if (!StringUtils.isNull(sign)) {
            return AjaxResult.success(sign);
        }
        return AjaxResult.error();
    }

    @GetMapping("list")
    @PreAuthorize("@ss.hasPermi('mobile:member:list')")
    public TableDataInfo queryMemberInfo(Member member, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        IPage<Member> memberIPage = memberService.queryMemberInfo(member, pageNum, pageSize);
        List<Member> records = memberIPage.getRecords();
        return getDataTable(records);
    }

    @PostMapping("add")
    public AjaxResult add() {

        return toAjax(1);
    }


    /**
     * 通过会员ID查询会员信息
     *
     * @return
     */
    @GetMapping("selectMemberById/{memberId}")
    public MemberVo selectMemberById(@PathVariable Integer memberId) {
        return memberService.selectMemberById(memberId);
    }

}
